package br.com.locanet.view;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import br.com.locanet.bean.IEntity;
import br.com.locanet.dao.GenericJPADAO;
import br.com.locanet.service.AbstractEntityService;
import br.com.locanet.util.UtilReflection;

public abstract class AbstractManagedBean<ENTITY extends IEntity, SERVICE extends AbstractEntityService<ENTITY, ? extends GenericJPADAO<ENTITY>>>{
	public static final String INSERT="insert";
	public static final String UPDATE="update";
	public static final String REMOVE="remove";
	public static final String SEARCH="search";
    private ENTITY entity;
    private List<ENTITY> entities;
    private SERVICE entityService;
	
	protected void addMessage(String message){
        addMessage(null, message);
    }

    protected void addMessage(String clientId, String message){
        getFacesContext().addMessage(clientId, new FacesMessage(message));
    }

    protected void addErrorMessage(String error){
        addMessage("errorMessage", error);
    }

    protected void addAlertMessage(String alert){
        addMessage("alertMessage", alert);
    }

    protected void addSuccessMessage(String success){
        addMessage("successMessage", success);
    }

    protected HttpServletRequest getRequest(){
        return (HttpServletRequest) getFacesContext().getExternalContext().getRequest();
    }

    protected HttpSession getSession(){
        return (HttpSession) getFacesContext().getExternalContext().getSession(false);
    }

    protected FacesContext getFacesContext(){
        return FacesContext.getCurrentInstance();
    }

	@SuppressWarnings("unchecked")
	public ENTITY getEntity() {
		if (entity==null){
			try{
				setEntity(((ENTITY) UtilReflection.getGenericClassType(getClass()).newInstance()));
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return entity;
	}

	public void setEntity(ENTITY entity) {
		this.entity = entity;
	}

	public List<ENTITY> getEntities() {
		if (entities==null){
			setEntities(getEntityService().selectAll());
		}
		return entities;
	}

	public void setEntities(List<ENTITY> entities) {
		this.entities = entities;
	}

	@SuppressWarnings("unchecked")
	protected SERVICE getEntityService() {
		if (entityService==null){
			try {
				setEntityService(((SERVICE) UtilReflection.getGenericClassType(getClass()).newInstance()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return entityService;
	}

	protected void setEntityService(SERVICE entityService) {
		this.entityService = entityService;
	}
	
	public String insert() throws Exception{
		entity=getEntityService().insert(entity);
		return INSERT;
	}
	public String update() throws Exception{
		entity=getEntityService().update(entity);
		return UPDATE;
	}
	public String remove() throws Exception{
		getEntityService().delete(entity);
		return REMOVE;
	}
	public String search() throws Exception{
		setEntities(getEntityService().selectAll());
		return SEARCH;
	}
}
